package com.sky.controller.user;

import com.sky.dto.OrdersPageQueryDTO;
import com.sky.dto.OrdersPaymentDTO;
import com.sky.dto.OrdersSubmitDTO;
import com.sky.result.PageResult;
import com.sky.result.Result;
import com.sky.service.OrderService;
import com.sky.vo.OrderPaymentVO;
import com.sky.vo.OrderSubmitVO;
import com.sky.vo.OrderVO;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController
@Slf4j
@RequestMapping("/user/order")
public class OrderController {
    @Autowired
    OrderService orderService;

    @PostMapping("/submit")
    public Result addOrder(@RequestBody OrdersSubmitDTO ordersSubmitDTO) {
        OrderSubmitVO orderVO = orderService.addOrder(ordersSubmitDTO);
        return Result.success(orderVO);
    }

    /**
     * 订单支付
     *
     * @param ordersPaymentDTO
     * @return
     */
    @PutMapping("/payment")
    @ApiOperation("订单支付")
    public Result<OrderPaymentVO> payment(@RequestBody OrdersPaymentDTO ordersPaymentDTO) throws Exception {
        log.info("订单支付：{}", ordersPaymentDTO);
        OrderPaymentVO orderPaymentVO = orderService.payment(ordersPaymentDTO);
        log.info("生成预支付交易单：{}", orderPaymentVO);
        return Result.success(orderPaymentVO);
    }

    @PostMapping("/repetition/{id}")
    public Result again(@PathVariable Long id) {
        orderService.again(id);
        return Result.success();
    }

    @GetMapping("/historyOrders")
    public Result PageOrder(OrdersPageQueryDTO queryDTO) {
        PageResult result = orderService.page(queryDTO);
        return Result.success(result);
    }

    @GetMapping("/orderDetail/{id}")
    public Result getDetailByID(@PathVariable Long id) {
        OrderVO orderVO =orderService.getDetailById(id);
        return Result.success(orderVO);
    }
    @PutMapping("/cancel/{id}")
    public Result cancelOrder(@PathVariable Long id){
      orderService.cancelOrder(id);
      return Result.success();
    }

    @PutMapping("/reminder/{id}")
    public Result Remind(@PathVariable Long id){
        orderService.reminder(id);
        return Result.success();
    }
}
